package com.hzw.saas.api.msg.bo;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.hzw.saas.common.util.Json;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * <p>
 * 消息详细业务接口信息
 * </p>
 *
 * @author sonam
 * @since 2021/1/7 0:07
 */
@Data
@ApiModel("消息详细业务接口信息")
@AllArgsConstructor
public class MsgActionBo implements Serializable {

    private static final long serialVersionUID = 6076794777506531063L;

    public MsgActionBo() {
    }

    @ApiModelProperty(value = "消息业务参数")
    private Map<String, Object> data;

    @ApiModelProperty(value = "请求方式", notes = "get/post/delete/put,如果为空默认post")
    private String methodType;

    @ApiModelProperty(value = "业务接口")
    private String action;

    @ApiModelProperty(value = "业务名称", notes = "一般为该条信息的按钮显示的文本")
    private String actionName;

    public static List<MsgActionBo> parse(String action) {
        try {
            // 解析业务文本
            List<MsgActionBo> msgActionList = Json.parseArray(action, MsgActionBo[].class);
            // 设置默认请求方式
            msgActionList.forEach(msgActionDto -> {
                String methodType = msgActionDto.getMethodType();
                if (!Objects.equals(methodType, "get") &&
                    !Objects.equals(methodType, "post") &&
                    !Objects.equals(methodType, "put") &&
                    !Objects.equals(methodType, "delete")
                ) {
                    msgActionDto.setMethodType("post");
                }
            });
            return msgActionList;
        } catch (Exception e) {
            return Collections.emptyList();
        }
    }

}
